<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入商品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="id">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="marketEnable">
        <el-select v-model="queryParams.marketEnable" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.mall_goods_market_enable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="销售模式" prop="salesModel">
        <el-select v-model="queryParams.salesModel" placeholder="请选择销售模式" clearable size="small">
          <el-option
            v-for="dict in dict.type.mall_goods_sales_model"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品类型" prop="goodsType">
        <el-select v-model="queryParams.goodsType" placeholder="请选择商品类型" clearable size="small" >
          <el-option
            v-for="dict in dict.type.mall_goods_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-bottom"
          size="mini"
          @click="handleDown"
          v-hasPermi="['mall-manager:goods:under']"
        >下架</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-top"
          size="mini"
          @click="handleUpper"
          v-hasPermi="['mall-manager:goods:upper']"
        >上架</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cms:news:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cms:news:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名称" align="center" width="250" prop="goodsName" >
        <template slot-scope="scope">
          <div style="margin: 5px 0px; height: 80px; display: flex">
            <div style="">
              <img
                :src="scope.row.original"
                style="height: 60px; margin-top: 1px; width: 60px"
              />
            </div>

            <div style="margin-left: 13px">
              <div class="div-zoom">
                <a  @click="$mall.linkTo(scope.row.id, scope.row.skuId)">{{ scope.row.goodsName }}</a>
              </div>
              <el-popover
                placement="top-start"
                title="扫码在手机中查看"
                width="200"
                trigger="hover"
                >
                <div style="text-align: center">
                  <vue-qr
                    :text="$mall.wapLinkTo(scope.row.id, scope.row.skuId)"
                    :margin="0"
                    colorDark="#000"
                    colorLight="#fff"
                    :size="150"
                  ></vue-qr>
                </div>
                <img
                  slot="reference"
                  src="../../../../assets/qrcode.svg"
                  class="hover-pointer"
                  width="20"
                  height="20"
                  alt=""
                  style="float: left"
                />
              </el-popover>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品编号" align="center" prop="id" />
      <el-table-column label="价格" align="center" prop="price" >
        <template slot-scope="scope">
          {{$mall.unitPrice(scope.row.price, '￥')}}
        </template>
      </el-table-column>
      <el-table-column label="销售模式" align="center" prop="newsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mall_goods_sales_model" :value="scope.row.salesModel"/>
        </template>
      </el-table-column>
      <el-table-column label="商品类型" align="center" prop="goodsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mall_goods_type" :value="scope.row.goodsType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="marketEnable" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mall_goods_market_enable" :value="scope.row.marketEnable"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="authFlag" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mall_goods_auth_flag" :value="scope.row.authFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" >
              <el-dropdown-item v-hasPermi="['mall-manager:goods:upper']" v-if="scope.row.marketEnable == 'DOWN'">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-top"
                  @click="handleUpper(scope.row)"
                >上架
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['mall-manager:goods:under']" v-if="scope.row.marketEnable == 'UPPER'">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-bottom"
                  @click="handleDown(scope.row)"
                >下架
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['mall-manager:goods:under']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-eye"
                  @click="handleDetail(scope.row)"
                >查看
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item  v-hasPermi="['mall-manager:goods:edit']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"

                >修改
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item command="a"   v-hasPermi="['project:project:remove']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"

                >删除
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--  下架商品  -->
    <el-dialog title="下架商品" :visible.sync="underopen" v-if="underopen" width="50%" @close="underopen = false" append-to-body>
      <el-form ref="underform" :model="underForm" :rules="underRules" label-width="80px">
        <el-form-item label="下架原因" prop="reason">
          <el-input v-model="underForm.reason" placeholder="请输入下架原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUnderForm">确 定</el-button>
        <el-button @click="underopen = false">取 消</el-button>
      </div>
    </el-dialog>

    <!--  商品详情  -->
    <el-dialog title="商品详情" :visible.sync="detailopen" v-if="detailopen" width="80%" @close="detailopen = false" append-to-body>
      <goods-detail :goodsId="goodsId"></goods-detail>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailopen = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, upGoods, underGoods } from "@/api/mall/goods/goods";
import GoodsDetail from './goods-detail'
import vueQr from "vue-qr";
export default {
  components: { vueQr,GoodsDetail},
  name: "goods",
  dicts: ['mall_goods_market_enable','mall_goods_sales_model','mall_goods_type','mall_goods_auth_flag'],
  data() {
    return {
      id: "", //要操作的id
      loading: true, // 表单加载状态
      modalVisible: false, // 添加或编辑显示
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        id: null,
        storeName: null,
        marketEnable: null,
        salesModel: null,
        goodsType: null,
      },
      underForm: {
        // 下架原因
        reason: "",
        goodsIds: undefined
      },
      // 表单校验
      underRules: {
        reason: [
          { required: true, message: "下架原因不能为空", trigger: "blur" },
          { min: 1, max: 60, message: '下架原因长度必须介于 1 和 60 之间', trigger: 'blur' }
        ],
      },
      submitLoading: false, // 添加或编辑提交状态
      total: 0, // 表单数据总数
      goodsList: [],
      underopen: false,
      detailopen: false,
      goodsId: undefined
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 上架操作  */
    handleUpper(row){
      const goodsIds = row.id ? [row.id] : this.ids
      let param = {
        goodsIds: goodsIds
      }
      this.$modal.confirm('是否确认上架商品编号为"' + goodsIds + '"的数据项？').then(function() {
        return upGoods(param);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("上架成功");
      }).catch(() => {});
    },
    /** 下架操作  */
    handleDown(row){
      this.underopen = true;
      const goodsIds = row.id ? [row.id] : this.ids
      this.resetUnderForm(goodsIds);
    },
    /** 查看商品详情 */
    handleDetail(row){
      this.goodsId = row.id;
      this.detailopen = true;
    },
    /** 商品修改 */
    handleUpdate(row){
      this.goodsId = row.id;
      this.$router.push({
        path: "/mall/goods/manager/operation",
        query: {
          id: row.id,
        }});
    },
    resetUnderForm(goodsIds){
      this.underForm = {
        reason: '',
        goodsIds: goodsIds,
      };
      this.resetForm("underform");
    },
    // 编辑
    edit(v) {
      this.id = v.id;
      if (v.underMessage) {
        this.underForm.reason = v.underMessage;
      } else {
        this.underForm.reason = "";
      }
      this.modalVisible = true;
    },
    // 下架
    submitUnderForm() {
      underGoods(this.underForm).then((res) => {
        this.$modal.msgSuccess("操作成功");
        this.underopen = false;
        this.getList();
      });
    },

    //查看商品详情
    showDetail(v) {
      let id = v.id;
      this.$router.push({
        name: "goods-detail",
        query: { id: id },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.div-zoom {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 4;
  overflow: hidden;
}
</style>
